<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\JumpTag;
use App\Constants\MiniProgramConstant;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class JumpTagController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new JumpTag(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id');
            $grid->column('name');
            $grid->column('type')->using(MiniProgramConstant::JUMP_MAP);
            $grid->column('is_show')->switch();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->like('name')->width(3);
                $filter->equal('type')->select(MiniProgramConstant::JUMP_MAP)->width(3);
                $filter->between('created_at')->datetime()->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new JumpTag(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type');
            $show->field('data');
            $show->field('jump_app_id');
            $show->field('jump_app_path');
            $show->field('is_show');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new JumpTag(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->select('type')->options(MiniProgramConstant::JUMP_MAP)
                ->when('mini_page', function (Form $form) {
                    $form->text('jump_page');
                })
                ->when('jump_mini', function (Form $form) {
                    $form->text('jump_app_id');
                    $form->text('jump_app_path');
                })
                ->required();

            $form->text('data')->saving(function ($v) {
                return (string)$v;
            });
            $form->radio('is_show')->options(['隐藏', '显示'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
